//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CumulativeNormalDistribution.h"
using namespace Cephei::QL::Math::Distributions;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (Microsoft::FSharp::Core::FSharpOption<Double>^ average, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCumulativeNormalDistribution = NULL;
#endif
        QuantLib::Real _average = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (average) ? (QuantLib::Real)ValueHelper::Convert (average->Value) : 0.0); //4
        QuantLib::Real _sigma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (sigma) ? (QuantLib::Real)ValueHelper::Convert (sigma->Value) : 1.0); //4
        _ppCumulativeNormalDistribution = new boost::shared_ptr<QuantLib::CumulativeNormalDistribution> (new QuantLib::CumulativeNormalDistribution ( _average,  _sigma ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (boost::shared_ptr<QuantLib::CumulativeNormalDistribution>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCumulativeNormalDistribution = NULL;
#endif
	_ppCumulativeNormalDistribution = &childNative;
    
}
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (QuantLib::CumulativeNormalDistribution& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCumulativeNormalDistribution = NULL;
#endif
	_ppCumulativeNormalDistribution = new boost::shared_ptr<QuantLib::CumulativeNormalDistribution> (&childNative);
    
    _CumulativeNormalDistributionOwner = owner;
    
}

Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (CCumulativeNormalDistribution^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCumulativeNormalDistribution = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCumulativeNormalDistribution = new boost::shared_ptr<QuantLib::CumulativeNormalDistribution> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCumulativeNormalDistribution = NULL;
#endif
	if (!t->IsSubclassOf(CCumulativeNormalDistribution::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (QuantLib::Handle<QuantLib::CumulativeNormalDistribution>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCumulativeNormalDistribution = &childNative;
	_ppCumulativeNormalDistribution = &static_cast<boost::shared_ptr<QuantLib::CumulativeNormalDistribution>>(childNative.currentLink());
    
    _CumulativeNormalDistributionOwner = owner;
}
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (QuantLib::Handle<QuantLib::CumulativeNormalDistribution> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCumulativeNormalDistribution = &childNative;
	_ppCumulativeNormalDistribution = &static_cast<boost::shared_ptr<QuantLib::CumulativeNormalDistribution>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::CCumulativeNormalDistribution (QuantLib::CumulativeNormalDistribution childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCumulativeNormalDistribution = NULL;
#endif
	_ppCumulativeNormalDistribution = new boost::shared_ptr<QuantLib::CumulativeNormalDistribution> (new QuantLib::CumulativeNormalDistribution (childNative));
    
}
#endif

Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::~CCumulativeNormalDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCumulativeNormalDistribution != NULL)
    {
	    delete _ppCumulativeNormalDistribution;
        _ppCumulativeNormalDistribution = NULL;
    }
}
Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::!CCumulativeNormalDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCumulativeNormalDistribution != NULL)
    {
	    delete _ppCumulativeNormalDistribution;
    }
}
QuantLib::CumulativeNormalDistribution& Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::GetReference ()
{
    if (_ppCumulativeNormalDistribution == NULL) throw REFNEW NativeNullException ();
	return **_ppCumulativeNormalDistribution;
}
boost::shared_ptr<QuantLib::CumulativeNormalDistribution>& Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::GetShared ()
{
    if (_ppCumulativeNormalDistribution == NULL) throw REFNEW NativeNullException ();
	return *_ppCumulativeNormalDistribution;
}
QuantLib::CumulativeNormalDistribution* Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::GetPointer ()
{
    if (_ppCumulativeNormalDistribution == NULL) throw REFNEW NativeNullException ();
	return &**_ppCumulativeNormalDistribution;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CumulativeNormalDistribution>& Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::GetHandle ()
{
	if (_phCumulativeNormalDistribution == NULL)
	{
		_phCumulativeNormalDistribution = new Handle<QuantLib::CumulativeNormalDistribution> (*_ppCumulativeNormalDistribution);
	}
	return *_phCumulativeNormalDistribution;
}
#endif
bool Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::HasNative () 
{
	return (_ppCumulativeNormalDistribution != NULL);
}

Double Cephei::QL::Math::Distributions::CCumulativeNormalDistribution::Derivative (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCumulativeNormalDistribution)->derivative ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Distributions::ICumulativeNormalDistribution^ Cephei::QL::Math::Distributions::CCumulativeNormalDistribution_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ average, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma)
{
    return REFNEW CCumulativeNormalDistribution ( average,  sigma);
}
